<?php
/**
 * Created by PhpStorm.
 * User: zwh
 * Time: 2017-3-28 09:55:05
 */
class SceUsersController extends BaseController {
    /*
     * 新技师登陆接口用于查看微信支付状态
     * 
     */
    public function login(){
        $validator = Validator::make(Input::all(),
            array(
                'account' => 'required',
                'passwd' => 'required',
                'imei' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $account = Input::get('account');
            $passwd = strtoupper(md5(Input::get('passwd')));
            $imei = Input::get('imei');

            //验证密码
            // $engineer = MaintenanceEngineers::where('account',$account)
            //     ->where('passwd',$passwd)
            //     ->where('status',1)
            //     ->where('type','shop')
            //     ->select(array('id','name','title','skill_level','type','type_id'))
            //     ->first();
            $engineer = SCUser::where('user_name',$account)
                ->where('password',$passwd)
                ->where('status',1)
                ->select(array('id','title'))
                ->first();
            if(!is_null($engineer)){                
                if(!in_array($engineer->title,array(1,2))){
                    $response = Lang::get("sce_error.login_error_1022");
                    break;
                }
                //记录技师token，到数据库
                try {
                    EngineerTokensWx::where('engineer_id',$engineer->id)->delete();
                    $engineer_token = new EngineerTokensWx;
                    $engineer_token->engineer_id = $engineer->id;
                    $engineer_token->token = md5($account.time().rand(0,100));
                    $engineer_token->work_station_num =0;
                    $engineer_token->created_at = Common::num2DateTime(time());
                    $engineer_token->out_time_at = Common::num2DateTime(time() + Config::get("engineer.token_time_out"));
                    $engineer_token->save();

                    // //记录登录信息 暂时只更新登录时间
                    // $m=MaintenanceEngineers::find($engineer->id);
                    // $m->last_login_at=Common::num2DateTime(time());
                    // $m->save();
                } catch (Exception $e) {
                    Log::error("error", array("system" => "sce" ,"event" => "login", 'input' =>Input::all() ,"exception" => $e));
                    $response = Lang::get("sce_error.login_create_token_error_1003");
                    break;
                }
            }else{
                $response = Lang::get("sce_error.login_not_exist_error_1002");
                break;
            }

            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array(
                    'token' => $engineer_token->token,
                    'expired' => $engineer_token->out_time_at,
                    'shop' => array(
                        'shop_id' => "",
                        'shop_name' => ""
                    ),
                    'device' => array(
                        'device_id' => "",
                        'device_name' => "",
                        'work_station_num' => "",
                        'work_name' =>""
                    ),
                    'engineer' => array(
                        'id' => "",
                        'name' => "",
                        'title' => "",
                        'sidkill_level' => "",
                    ),
                )
            );

        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "login", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 技师登出
     * 
     */
    public function logout(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }

            //传入参数
            $token = Input::get('token');

            //验证token
            $engineer_data = MaintenanceEngineers::getEngineerByTokenWx($token);
            if(is_numeric($engineer_data) && $engineer_data < 0){
                $response = Lang::get("sce_error.user_token_not_exist_1004");
                break;
            }
            try{
                EngineerTokensWx::where('engineer_id',$engineer_data->id)->delete();
            } catch (Exception $e) {
                Log::error("error", array("system" => "sce" ,"event" => "logout", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("sce_error.user_token_clean_error_1005");
                break;
            }

            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array(
                    'id' => $engineer_data->id
                )
            );

        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "logout", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}